package coms.ejb;

import coms.dataaccess.dao.GenericDAO;
import java.lang.reflect.ParameterizedType;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import coms.dataaccess.dao.*;
import coms.dataaccess.hibernate.GenericDAOImpl;

public class GenericBean<T> implements GenericBeanRemote<T> {

    private Class<T> persistentClass;
    //private Class<? extends GenericDAO> daoClass;
    private GenericDAO daoClass;

    public void construct() {

        this.persistentClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        try {

            System.out.println("persistentClass.getClass().getCanonicalName() = " + persistentClass.getCanonicalName());
            System.out.println("persistentClass.getClass().getName() = " + persistentClass.getName());
            String temp=persistentClass.getName();
            
            temp=temp.replace("dto","dataaccess.hibernate");
            System.out.println(temp);
            
            //daoClass = (Class<? extends GenericDAO>) Class.forName(persistentClass.getClass().getName() + "DAO").newInstance();
            System.out.println("hulalalalaa");
            Class cls = Class.forName(temp + "DAOImpl");
            System.out.println("cls = " + cls);
            
            Object obj = cls.newInstance();
            System.out.println("obj = " + obj);
            
            daoClass = (GenericDAOImpl) obj;
        } catch (IllegalAccessException ex) {
            Logger.getLogger(GenericBean.class.getName()).log(Level.SEVERE, null, ex);

        } catch (InstantiationException ex) {
            Logger.getLogger(GenericBean.class.getName()).log(Level.SEVERE, null, ex);

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(GenericBean.class.getName()).log(Level.SEVERE, null, ex);
        }

    }


    @Override
    public List<T> findAll() {
        List<T> tList = null;
        try {
            tList = daoClass.findAll();
        } catch (SQLException ex) {
            Logger.getLogger(GenericBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return tList;
    }

    @Override
    public T create(T t) {
        try {
            t = (T) daoClass.create(t);
        } catch (SQLException ex) {
            Logger.getLogger(GenericBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch(Exception ex){
               Logger.getLogger(GenericBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return t;
    }

    @Override
    public boolean update(T t) {
        boolean success = false;
        try {
            success = daoClass.update(t);
        } catch (SQLException ex) {
            Logger.getLogger(GenericBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return success;
    }

    @Override
    public boolean delete(T t) {
        boolean success = false;
        try {
            success = daoClass.delete(t);
        } catch (SQLException ex) {
            Logger.getLogger(GenericBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return success;
    }

    @Override
    public T getById(int id) {
        T t = null;
        try {
            t = (T) daoClass.getById(id);
        } catch (SQLException ex) {
            Logger.getLogger(GenericBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return t;
    }

    @Override
    public List<T> search(T t) {
        List<T> tList = null;
        try {
            tList = daoClass.search(t);
        } catch (SQLException ex) {
            Logger.getLogger(GenericBean.class.getName()).log(Level.SEVERE, null, ex);
        }
         return tList;
    }
}
